/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.daoImpl;

import br.com.deployment.dao.OrganDAO;
import br.com.deployment.entities.Organ;
import br.com.deployment.genericdao.GenericDAO;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author luciano
 */
@Stateless
@LocalBean
public class OrganDAOImpl extends GenericDAO<Organ> implements OrganDAO{

    public OrganDAOImpl() {
        super(Organ.class);
    }

    @Override
    public List<Organ> findByName(String name) {
       List<Organ> lista = new ArrayList<Organ>();
       String sql = "Select o from Organ o where lower(o.name) like lower(:name)";
       Query query = getEntityManager().createQuery(sql);
       
        if (name !=  null) {
                query.setParameter("name", "%"+ name +"%");
        }
        
       lista = query.getResultList();
       return lista;
    }

    @Override
    public List<Organ> find(Organ organ) {
         List<Organ> lista = new ArrayList<Organ>();

        String sql = "SELECT o FROM Organ o where 1=1";


        if ((organ.getName() != null) && (! organ.getName().equals(""))) {
            sql += " and lower(o.name) like lower(:name)";
        }
        if ((organ.getCity() != null) && (organ.getCity().getName() != null )) {
            sql += " and lower(o.city.name) like lower(:city)";
        }
        
        Query query = getEntityManager().createQuery(sql);
        
        if  ((organ.getName() != null) && (! organ.getName().equals(""))){
            query.setParameter("name", "%" + organ.getName() + "%");
        }
          if ((organ.getCity() != null) && (organ.getCity().getName()!= null)) {
            query.setParameter("city", "%" + organ.getCity().getName() + "%");
        }
        lista = query.getResultList();
        return lista;
    }


}
